﻿@if (LabelStyle == FormLabelStyle.Header)
{
    <label for="@Id" class="mb-4 text-xl font-semibold dark:text-white block">@Label</label>
}
else
{
    <label for="@Id" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">@Label</label>
}
<div class="relative">
    @if (Type == "textarea")
    {
        <textarea id="@Id" style="height: 200px;" class="outline-0 shadow-sm bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg block w-full p-2.5 pr-10 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" value="@Value" @oninput="OnInputChanged" placeholder="@Placeholder" autocapitalize="off" autocorrect="off"></textarea>
    }
    else
    {
        <input type="@Type" id="@Id" autocomplete="off" @onfocus="OnFocusEvent" class="outline-0 shadow-sm bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg block w-full p-2.5 pr-10 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" value="@Value" @oninput="OnInputChanged" placeholder="@Placeholder" autocapitalize="off" autocorrect="off">
    }
</div>

@code {
    /// <summary>
    /// Defines the style for the form label.
    /// </summary>
    public enum FormLabelStyle
    {
        /// <summary>
        /// Default label style - small font, medium weight.
        /// </summary>
        Default,

        /// <summary>
        /// Header label style - large font, semibold weight.
        /// </summary>
        Header
    }
    /// <summary>
    /// Id for the input field.
    /// </summary>
    [Parameter]
    public string Id { get; set; } = string.Empty;

    /// <summary>
    /// Label for the input field.
    /// </summary>
    [Parameter]
    public string Label { get; set; } = "Value";

    /// <summary>
    /// Type of input field.
    /// </summary>
    [Parameter]
    public string Type { get; set; } = "text";

    /// <summary>
    /// Value of the input field.
    /// </summary>
    [Parameter]
    public string Value { get; set; } = string.Empty;

    /// <summary>
    /// Callback that is triggered when the value changes.
    /// </summary>
    [Parameter]
    public EventCallback<FocusEventArgs> OnFocus { get; set; }

    /// <summary>
    /// Callback that is triggered when the value changes.
    /// </summary>
    [Parameter]
    public EventCallback<string?> ValueChanged { get; set; }

    /// <summary>
    /// Placeholder text for the input field.
    /// </summary>
    [Parameter]
    public string Placeholder { get; set; } = string.Empty;

    /// <summary>
    /// Style of the label.
    /// </summary>
    [Parameter]
    public FormLabelStyle LabelStyle { get; set; } = FormLabelStyle.Default;

    private async Task OnInputChanged(ChangeEventArgs e)
    {
        Value = e.Value?.ToString() ?? string.Empty;
        await ValueChanged.InvokeAsync(Value);
    }

    private async Task OnFocusEvent(FocusEventArgs e)
    {
        if (OnFocus.HasDelegate)
        {
            await OnFocus.InvokeAsync(e);
        }
    }
}
